package com.yin.myproject;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
 * Class对象:字段(Field)相关API
 *
 * @author Eason
 * @create 2018-03-30 16:19
 **/
public class ClassAPIForField {
    public static void main(String[] args) {
        //通过.class字面量的形式获取Class对象
        Class<User> userClzz = User.class;

        System.out.println("================================getFields======================================================");

        //getFields()获取当前类及其父类的所有public类型的字段
        Field[] fields = userClzz.getFields();
        for (Field field : fields) {
            System.out.println("字段名称:" + field.getName() + ".字段类型:" + field.getType()+",字段修饰符:"+ Modifier.toString(field.getModifiers()));
        }

        System.out.println("================================getDeclaredFields======================================================");

        //userClzz.getDeclaredFields()获取当前类的public protected private default类型所有字段 但不包括继承自父类的字段
        Field[] fields1 = userClzz.getDeclaredFields();
        for (Field field : fields1) {
            System.out.println("字段名称:" + field.getName() + ".字段类型:" + field.getType()+",字段修饰符:"+field.getModifiers());
        }


        System.out.println("==================================getField=======================================================");


        try {
            //返回一个 Field 对象，它反映此 Class 对象所表示的类或接口的指定公共成员字段。
            System.out.println("获取当前类私有成员");
            Field field = userClzz.getField("id");
            System.out.println("字段名称:" + field.getName() + ".字段类型:" + field.getType()+",字段修饰符:"+field.getModifiers());
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        try {
            System.out.println("获取当前类共有有成员");
            Field field1 = userClzz.getField("firstName");
            System.out.println("字段名称:" + field1.getName() + ".字段类型:" + field1.getType()+",字段修饰符:"+field1.getModifiers());
        } catch (Exception e){
            e.printStackTrace();
        }
        try {
            System.out.println("获取当父类public field");
            Field field2 = userClzz.getField("sex");
            System.out.println("字段名称:" + field2.getName() + ".字段类型:" + field2.getType()+",字段修饰符:"+field2.getModifiers());
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        System.out.println("================================getDeclaredField======================================================");

        //返回一个 Field 对象，该对象反映此 Class 对象所表示的类或接口的指定已声明字段（包括私有成员）。
        try {
            System.out.println("获取当前类私有成员");
            Field field = userClzz.getDeclaredField("id");
            System.out.println("字段名称:" + field.getName() + ".字段类型:" + field.getType()+",字段修饰符:"+field.getModifiers());
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        try {
            System.out.println("获取当前类public成员");
            Field field2 = userClzz.getDeclaredField("firstName");
            System.out.println("字段名称:" + field2.getName() + ".字段类型:" + field2.getType()+",字段修饰符:"+field2.getModifiers());
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        try {
            System.out.println("获取当父类public field");
            Field field2 = userClzz.getDeclaredField("sex");
            System.out.println("字段名称:" + field2.getName() + ".字段类型:" + field2.getType()+",字段修饰符:"+field2.getModifiers());
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

    class Person {
        public Boolean sex;
        protected String name;
        private Integer age;

        public Boolean getSex() {
            return sex;
        }

        public void setSex(Boolean sex) {
            this.sex = sex;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }
    }

    class User extends Person {
        public String firstName;
        protected String middleName;
        private String id;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getMiddleName() {
            return middleName;
        }

        public void setMiddleName(String middleName) {
            this.middleName = middleName;
        }
    }
}
